package cn.fintecher.pangolin.service.manage.web;

import cn.fintecher.pangolin.common.enums.BaseSelect;
import cn.fintecher.pangolin.common.response.LoginResponse;
import cn.fintecher.pangolin.common.web.BaseController;
import cn.fintecher.pangolin.entity.mysql.QUser;
import cn.fintecher.pangolin.entity.mysql.User;
import cn.fintecher.pangolin.service.manage.repository.UserRepository;
import cn.fintecher.pangolin.service.manage.request.AddDepartmentRequest;
import cn.fintecher.pangolin.service.manage.request.ModifyDepartmentRequest;
import cn.fintecher.pangolin.service.manage.response.CollectorResponse;
import cn.fintecher.pangolin.service.manage.response.DepartmentResponse;
import cn.fintecher.pangolin.service.manage.service.BaseService;
import cn.fintecher.pangolin.service.manage.service.DepartmentService;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: xiaqun
 * @Description: 部门相关接口
 * @Date: 16:42 2019/8/14
 */

@RestController
@RequestMapping("/api/departmentController")
@Api(value = "部门相关接口", description = "部门相关接口")
public class DepartmentController extends BaseController {
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private BaseService baseService;

    @Autowired
    private UserRepository userRepository;

    /**
     * 新增部门
     */
    @ApiOperation(value = "新增部门", notes = "新增部门")
    @PostMapping("/addDepartment")
    public ResponseEntity<Void> addDepartment(@Valid @RequestBody AddDepartmentRequest request,
                                              @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to add department,{}", request);
        departmentService.addDepartment(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改部门
     */
    @ApiOperation(value = "修改部门", notes = "修改部门")
    @PutMapping("/modifyDepartment")
    public ResponseEntity<Void> modifyDepartment(@Valid @RequestBody ModifyDepartmentRequest request,
                                                 @RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to modify department,{}", request);
        departmentService.modifyDepartment(request, token);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除部门
     */
    @ApiOperation(value = "删除部门", notes = "删除部门")
    @DeleteMapping("/deleteDepartment")
    public ResponseEntity<Void> deleteDepartment(@RequestParam @ApiParam(value = "部门ID", required = true) String departId) {
        log.info("REST request to delete department,{}", departId);
        departmentService.deleteDepartment(departId);
        return ResponseEntity.ok().build();
    }

    /**
     * 获取全部部门
     */
    @ApiOperation(value = "获取全部部门", notes = "获取全部部门")
    @GetMapping("/findAllDepartment")
    public ResponseEntity<List<DepartmentResponse>> findAllDepartment(@RequestHeader(value = "X-UserToken") String token) {
        log.info("REST request to find all department");
        List<DepartmentResponse> list = departmentService.findAllDepartment(token);
        return ResponseEntity.ok().body(list);
    }

    @ApiOperation(value = "获取所有的分公司", notes = "获取所有的分公司")
    @GetMapping("/findAllBranch")
    public ResponseEntity<List<DepartmentResponse>> findAllBranch() {
        log.info("find all Branch");
        List<DepartmentResponse> allBranch = departmentService.findAllBranch();
        return ResponseEntity.ok().body(allBranch);
    }

    @ApiOperation(value = "根据组织机构code获取催收员", notes = "根据组织机构Id获取催收员")
    @GetMapping(value = "/queryCollectorByDepartCode")
    public ResponseEntity<List<CollectorResponse>> queryCollectorByDepartCode(@RequestHeader(value = "X-UserToken") String token) {
        LoginResponse userSessionByToken = baseService.getUserSessionByToken(token);
        String departmentCode = userSessionByToken.getDepartmentModel().getDepartCode();
        BooleanBuilder booleanBuilder = new BooleanBuilder();
        booleanBuilder.and(QUser.user.department.departCode.like(StringUtils.join(departmentCode, "%")));
        booleanBuilder.and(QUser.user.isManage.eq(BaseSelect.NO));
        List<User> users = Lists.newArrayList(userRepository.findAll(booleanBuilder));
        List<CollectorResponse> collectorResponseList = new ArrayList<>();
        if (!users.isEmpty()) {
            users.forEach(user -> {
                CollectorResponse collectorResponse = new CollectorResponse();
                collectorResponse.setUserId(user.getId());
                collectorResponse.setRealName(user.getRealName());
                collectorResponseList.add(collectorResponse);
            });
        }
        return new ResponseEntity<>(collectorResponseList, HttpStatus.OK);
    }
}
